package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.ApplyRefundReq;
import com.ymatou.iapi.optrade.parameter.req.ChangeRefundBillReq;
import com.ymatou.iapi.optrade.parameter.vo.SalesRefundInfoVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ChangeSalesRefundInfoCall;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 更新退货退款信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_ChangeSalesRefundInfo {
	private static ChangeRefundBillReq changeRefundBillReq;
	private static ChangeSalesRefundInfoCall changeSalesRefundInfoCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新退货退款信息接口");
	}

	@Before
	public void caseUp() {
		changeRefundBillReq = new ChangeRefundBillReq();
		changeSalesRefundInfoCall = new ChangeSalesRefundInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeSalesRefundInfoCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_001() {
		Logger.start(true, "更新退货退款信息成功");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.placeOrder3(true);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();
			Long orderId = order.getOrderId();

			long sellerId = order.getSellerId();

			BigDecimal orderFreight = order.getOrderFreight();

			BigDecimal proFreight = order.getOrderDetailPoList().get(0)
					.getFreight();

			//支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			//申请改订单下第一个商品退款
			ApplyRefundReq applyRefundReq = RefundService
					.buildStandApplyRefundReq(order);

			SalesRefundInfoVo oldRefundInfoVo = applyRefundReq
					.getSalesRefundInfoList().get(0);

			applyRefundReq.setSalesRefundInfoList(Arrays
					.asList(oldRefundInfoVo));

			RefundService.applyRefundCall(applyRefundReq);

			changeRefundBillReq.setOrderId(orderId);
			changeRefundBillReq.setRefundBillNo(oldRefundInfoVo
					.getRefundBillNo());

			changeRefundBillReq.setRefundAmount(oldRefundInfoVo
					.getRefundAmount().add(orderFreight.subtract(proFreight)));

			changeRefundBillReq.setRefundProductNum(oldRefundInfoVo
					.getRefundProductNum());

			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");

			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口返回正常");

			DataUtil.verifyDecimalEquals(orderFreight, BigDecimal
					.valueOf((Double) changeSalesRefundInfoCall.getData().get(
							"refundFreight")), "验证refundFreight");

			String refundBillNo = oldRefundInfoVo.getRefundBillNo();

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(oldRefundInfoVo.getCatalogId());
			expectedRefundBillPo.setTotalAmount(oldRefundInfoVo
					.getRefundAmount().add(orderFreight.subtract(proFreight)));

			expectedRefundBillPo.setProductCount(oldRefundInfoVo
					.getRefundProductNum());

			expectedRefundBillPo.setFreight(orderFreight);

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_002() {
		Logger.start(false, "订单号不存在");
		try {
			changeRefundBillReq.setOrderId(111111);
			changeRefundBillReq.setRefundBillNo("1233");
			changeRefundBillReq.setRefundAmount(new BigDecimal(100));
			changeRefundBillReq.setRefundProductNum(100);
			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");
			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals(ECode.BCODE6,
					changeSalesRefundInfoCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals("订单不存在:" + changeRefundBillReq.getOrderId(),
					changeSalesRefundInfoCall.getString("msg"), "验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_003() {
		Logger.start(false, "退款单号不存在");
		try {
			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			// 申请退款
			changeRefundBillReq.setOrderId(orderId);
			changeRefundBillReq.setRefundBillNo("1111111");
			changeRefundBillReq.setRefundAmount(new BigDecimal(1));
			changeRefundBillReq.setRefundProductNum(100);
			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");
			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals(ECode.BCODE7,
					changeSalesRefundInfoCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals(
					"退款单不存在:" + changeRefundBillReq.getRefundBillNo(),
					changeSalesRefundInfoCall.getString("msg"), "验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_004() {
		Logger.start(false, "申请退款金额大于实际可退款金额");
		try {
			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			Long orderId = order.getOrderId();

			changeRefundBillReq.setOrderId(orderId);
			changeRefundBillReq.setRefundBillNo("TK0" + orderId);
			changeRefundBillReq.setRefundAmount(placeOrderResp
					.getPayableAmount().add(BigDecimal.TEN));

			changeRefundBillReq.setRefundProductNum(1);
			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");
			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals("114",
					changeSalesRefundInfoCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals("申请退款金额不能大于实际可退款金额",
					changeSalesRefundInfoCall.getString("msg"), "验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_005() {
		Logger.start(false, "退款单状态不正确,修改退款单失败");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			//申请改订单下第一个商品退款
			ApplyRefundReq applyRefundReq = RefundService
					.applyRefund(placeOrderResp.getOrderList().get(0));

			RefundService.approveRefundBillCall(applyRefundReq);

			String refundBillNo = applyRefundReq.getSalesRefundInfoList()
					.get(0).getRefundBillNo();
			changeRefundBillReq.setOrderId(placeOrderResp.getOrderList().get(0)
					.getOrderId());
			changeRefundBillReq.setRefundBillNo(refundBillNo);
			changeRefundBillReq.setRefundAmount(new BigDecimal(1));
			changeRefundBillReq.setRefundProductNum(100);
			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");
			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals("102",
					changeSalesRefundInfoCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals(
					"退款单已被审核:" + changeRefundBillReq.getRefundBillNo(),
					changeSalesRefundInfoCall.getString("msg"), "验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_006() {
		Logger.start(true, "更新退款的金额大于商品价格+订单总运费");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			// 申请退款
			String refundBillNo = "TK0" + orderId;

			BigDecimal oriRefund = placeOrderResp.getPayableAmount();

			changeRefundBillReq.setOrderId(orderId);
			changeRefundBillReq.setRefundBillNo(refundBillNo);
			changeRefundBillReq.setRefundAmount(oriRefund.add(BigDecimal.ONE));
			changeRefundBillReq.setRefundProductNum(1);
			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");
			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口返回正常");

			Logger.verifyEquals("申请退款金额不能大于实际可退款金额",
					changeSalesRefundInfoCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfo_007() {
		Logger.start(true, "更新商品退款金额加上部分商品运费");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			Order order = placeOrderResp.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			// 申请退款
			String refundBillNo = "TK0" + orderId;

			BigDecimal oriRefund = placeOrderResp.getPayableAmount();

			changeRefundBillReq.setOrderId(orderId);
			changeRefundBillReq.setRefundBillNo(refundBillNo);

			changeRefundBillReq.setRefundAmount(oriRefund
					.subtract(BigDecimal.ONE));

			changeRefundBillReq.setRefundProductNum(order
					.getOrderDetailPoList().get(0).getProductCount());

			changeRefundBillReq.setRefundType(1);
			changeRefundBillReq.setReason("123456789");
			changeSalesRefundInfoCall.setData(changeRefundBillReq);
			changeSalesRefundInfoCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changeSalesRefundInfoCall.getString("code"),
					"验证更新退货退款接口返回正常");

			DataUtil.verifyDecimalEquals(
					order.getOrderFreight().subtract(BigDecimal.ONE),
					BigDecimal.valueOf((Double) changeSalesRefundInfoCall
							.getData().get("refundFreight")), "验证refundFreight");

			RefundBillPo expectedRefundBillPo = new RefundBillPo();

			expectedRefundBillPo.setRefundBillNo(refundBillNo);
			expectedRefundBillPo.setCatalogId(placeOrderResp.getOrderList()
					.get(0).getOrderDetailPoList().get(0).getCatalogId());
			expectedRefundBillPo.setTotalAmount(changeRefundBillReq
					.getRefundAmount());

			expectedRefundBillPo.setProductCount(changeRefundBillReq
					.getRefundProductNum());

			expectedRefundBillPo.setFreight(order.getOrderFreight().subtract(
					BigDecimal.ONE));

			expectedRefundBillPo.setOrderId(orderId);
			expectedRefundBillPo.setUserId(userId);
			expectedRefundBillPo.setSellerId(sellerId);
			expectedRefundBillPo.setTopspeedRefund(false);
			expectedRefundBillPo.setAfterReceiving(false);
			expectedRefundBillPo.setRefundStatus(0);

			expectedRefundBillPo.setRefundBillStatus(1);

			RefundBillPo actualRefundBillPo = orderShardingWapper
					.selectRefundBillByBillNo(refundBillNo);

			RefundService.verifyRefundBillDb(expectedRefundBillPo,
					actualRefundBillPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
